/**
 * 使用递归将数组转为树形结构
 * 父ID属性为parent
 */
export function array2Tree(array, parentId) {
  if (array.length === 0) {
    return [];
  }

  const result = [];
  for (let i = 0; i < array.length; i++) {
    const item = array[i];

    if (item.pid === parentId) {
      result.push(item);

      // 递归查看当前节点对应的子节点
      const children = array2Tree(array, item.id);
      if (children.length > 0) {
        item.children = children;
      }
    }
  }
  return result;
}
